﻿using Dapper;
using System.Data;
using Todo.Common.Models.Todo;

namespace Todo.DAO.Dapper
{
    // https://github.com/ericdc1/Dapper.SimpleCRUD

    public class TodoRepository
    {
        private readonly IDbConnectionFactory _dbConnectionFactory;

        public TodoRepository(IDbConnectionFactory dbConnectionFactory)
        {
            this._dbConnectionFactory = dbConnectionFactory;
        }

        public async Task<IList<TodoItemDBEntity>> GetTodoListAsync()
        {
            using IDbConnection conn = await _dbConnectionFactory.CreateConnectionAsync();

            var ret = await conn.GetListAsync<TodoItemDBEntity>();

            return ret.ToList();
        }

        public async Task<TodoItemDBEntity> GetTodoItemById(int id)
        {
            using IDbConnection conn = await _dbConnectionFactory.CreateConnectionAsync();

            var ret = await conn.GetAsync<TodoItemDBEntity>(id);

            var ret2 = await conn.GetListAsync<TodoItemDBEntity>("where title = @Title", new {  Title="看书" });

            return ret;
        }





    }
}
